package com.tumblr.anond2.solr.util;

import java.util.List;
import java.util.Map;
import org.apache.solr.common.SolrDocumentList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Joiner;
import com.google.common.collect.LinkedHashMultiset;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multiset;
import com.google.common.collect.Table;
import com.tumblr.anond2.solr.client.solrj.SolrConnection;
import com.tumblr.anond2.solr.client.solrj.SolrRDBClient;
import com.tumblr.anond2.solr.sql.SolrSQLCommand;

/**
 * TableOutputter.
 * 
 * 
 */
public class TableOutputter {

	final static Logger logger = LoggerFactory.getLogger(TableOutputter.class);

	public static void main(String[] args) {
		TableOutputter test = new TableOutputter();
		try {
			logger.info("TableOutputter");
			test.execute();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	public TableOutputter(){

	}

	public void execute() throws Exception{
		SolrConnection cnn = new SolrConnection("http://localhost:8983/solr");
		cnn.open();
		SolrSQLCommand cmd = new SolrSQLCommand();
		cmd.setQuery("*:*");
		Map<String, String> additionalParams = Maps.newHashMap();
		additionalParams.put("sort", "table_s asc, id asc");
		cmd.setAdditionalParams(additionalParams);
		SolrDocumentList docs = cmd.executeDocumentList();

		Table<String, String, Object> table = SolrRDBClient.parseTable(docs);

		List<String> list = Lists.newArrayList();
		for (String columnKey :table.columnKeySet()) {
			if (!list.contains(columnKey)){
				list.add(columnKey);
			}
		}
		String header = "id,";
		header = header + Joiner.on(",").join(list);
		//for (String l :list){
		//	header = header + l + ",";
		//}
		//header = header.substring(0, header.length() - 1);
		System.out.println(header);

		List<Object> vList = Lists.newArrayList();
		for (String rowKey : table.rowKeySet()) {
			for (String l :list){
				Object o = table.get(rowKey, l);
				Object val = (o != null) ? o : "";
				vList.add(val);
			}
			String detail = rowKey.toString() + ",";
			detail = detail + Joiner.on(",").join(vList);
			//for (Object vl :vList){
			//	detail = detail + vl + ",";
			//}
			//detail = detail.substring(0, detail.length() - 1);
			vList.clear();
			System.out.println(detail);
		}

		SolrSQLCommand cmd2 = new SolrSQLCommand();
		cmd2.setQuery("*:*");
		additionalParams.clear();
		additionalParams.put("sort", "table_s asc, id asc");
		additionalParams.put("rows", "1000");
		cmd2.setAdditionalParams(additionalParams);
		SolrDocumentList docs2 = cmd2.executeDocumentList();
		Table<String, String, Object> table2 = SolrRDBClient.parseTable(docs2);
		Multiset<String> mset = LinkedHashMultiset.create();
		for (String column : table2.columnKeySet()){
			if (column.equals("table_s")){
				Map<String, Object> map = table2.column(column);
				for (String key : map.keySet()){
					String value = (String) map.get(key);
					mset.add(value, 1);
				}
			}
		}
		for (Multiset.Entry<String> e :mset.entrySet()){
			System.out.println(e.getElement() + ":" + mset.count(e.getElement()));
		}
		
			/*
			for (String rowKey : table.rowKeySet()) {
				Map<String, Object> map = table.row(rowKey);
				for (String key : map.keySet()){
					Object o = map.get(key);
					String line = rowKey.toString() + " " + key.toString() + " " + o.toString();
					System.out.println(line);
				}
			}
			System.out.println(" ");
			for (String columnKey :table.columnKeySet()) {
				Map<String, Object> map = table.column(columnKey);
				for (String key : map.keySet()){
					Object o = map.get(key);
					String line = columnKey.toString() + " " + key.toString() + " " + o.toString();
					System.out.println(line);
				}
			}
			System.out.println(" ");
			for (Cell<String, String, Object> cell :table.cellSet()) {
					String row = cell.getRowKey();
					String column = cell.getColumnKey();
					Object o = cell.getValue();
					String line = row.toString() + " " + column.toString() + " " + o.toString();
					System.out.println(line);
			}
			System.out.println(table.get("008", "company_id_s"));
			table.put("008", "company_id_s", "XXX");
			System.out.println(table.get("008", "company_id_s"));
			*/
	}



}
